#include "mainwindow.h"
#include "initdb.h"
#include "bookdelegate.h"
//#include "Platform_Interface.h"
#include <QtWidgets>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
      isLoginMode(false),
      hasConnDevice(false),
      isScanning(false)
{
    setWindowTitle(tr("ISR Reader"));

//    if (!QSqlDatabase::drivers().contains("QMYSQL"))
//    {
//        QMessageBox::critical(
//                    this,
//                    "Unable to load database",
//                    "We are needs the QMYSQL driver"
//                    );
//        return;
//    }

//    // Initialize the database:
//    QSqlError err = initDb();
//    if (err.type() != QSqlError::NoError)
//    {
//        showError(err);
//        return;
//    }

    createActions();
    createToolbars();
    createPropertyPlane();
    createLogTabWidget();
    createWorkSpace();

    propertyDockWidget = new QDockWidget(this);
    propertyDockWidget->setWindowTitle(tr("Property"));
    propertyDockWidget->setFeatures(QDockWidget::DockWidgetClosable|QDockWidget::DockWidgetMovable);
    propertyDockWidget->setWidget(propertyPlane);
    propertyDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);
    addDockWidget(Qt::RightDockWidgetArea, propertyDockWidget);

    logDockWidget = new QDockWidget(this);
    logDockWidget->setWindowTitle(tr("Output"));
    logDockWidget->setFeatures(QDockWidget::DockWidgetClosable|QDockWidget::DockWidgetMovable);
    logDockWidget->setWidget(logTabWidget);
    logDockWidget->setAllowedAreas(Qt::TopDockWidgetArea|Qt::BottomDockWidgetArea);
    addDockWidget(Qt::BottomDockWidgetArea, logDockWidget);

    setCentralWidget(workspace);
}

MainWindow::~MainWindow()
{
}

void MainWindow::onLoginMode()
{
    isLoginMode = !isLoginMode;
    if(isLoginMode)
    {
        loginModeAction->setText(tr("Exit Login"));
        loginModeAction->setIcon(QIcon(":/icon/action/logoutmode.png"));
        showMessage("Entry login mode");
    }
    else
    {
        loginModeAction->setText(tr("Login Mode"));
        loginModeAction->setIcon(QIcon(":/icon/action/loginmode.png"));
        showMessage("Exit login mode");
    }
}

void MainWindow::onConnDevice()
{
    hasConnDevice = !hasConnDevice;
    if(hasConnDevice)
    {
        connDeviceAction->setText(tr("Disconnect Device"));
        connDeviceAction->setIcon(QIcon(":/icon/action/disconnect.png"));
        deviceStateImage->setChecked(false);
        showMessage("Has connect device");
    }
    else
    {
        connDeviceAction->setText(tr("Connect Device"));
        connDeviceAction->setIcon(QIcon(":/icon/action/conndevice.png"));
        deviceStateImage->setChecked(true);
        showMessage("No connect device");
    }
}

void MainWindow::onScanDevice()
{
    isScanning = !isScanning;
    if(isScanning)
    {
        scanDeviceAction->setText(tr("Stop Scan"));
        scanDeviceAction->setIcon(QIcon(":/icon/action/stopscan.png"));
        showMessage("start scan device");
    }
    else
    {
        scanDeviceAction->setText(tr("Start Scan"));
        scanDeviceAction->setIcon(QIcon(":/icon/action/startscan.png"));
        showMessage("stop scan device");
    }
}

void MainWindow::onConnDatabase()
{
    showMessage("connect database success");
}

void MainWindow::onLoadFormatFile()
{
    showMessage("load format file success");
}

void MainWindow::onEditLogicFile()
{
    showMessage("edit logic file finished");
}

void MainWindow::onAbout()
{
    QMessageBox::about(this, tr("About ISR Reader"),
                       tr("The <b>ISR Reader</b> is application "
                          "of the MayStar."));
}

void MainWindow::onDeviceIpAddrChanged(QString ipAddr)
{
    deviceIpAddrList.push_back(ipAddr);
}

void MainWindow::showPropertyPlane()
{
    propertyDockWidget->show();
}
void MainWindow::showLogWindow()
{
    logDockWidget->show();
}

void MainWindow::createActions()
{
    // file
    loginModeAction = new QAction(QIcon(":/icon/action/loginmode.png"),tr("Login Mode"), this);
    loginModeAction->setStatusTip(tr("Entry login mode"));
    connect(loginModeAction, SIGNAL(triggered()), this, SLOT(onLoginMode()));

    connDeviceAction = new QAction(QIcon(":/icon/action/conndevice.png"),tr("Connect Device"), this);
    connDeviceAction->setStatusTip(tr("Connect scan device"));
    connect(connDeviceAction, SIGNAL(triggered()), this, SLOT(onConnDevice()));

    scanDeviceAction = new QAction(QIcon(":/icon/action/startscan.png"), tr("Start Scan"), this);
    scanDeviceAction->setStatusTip(tr("Start scan device"));
    connect(scanDeviceAction, SIGNAL(triggered()), this, SLOT(onScanDevice()));

    // edit
    connDatabaseAction = new QAction(tr("Connect Database"), this);
    connDatabaseAction->setStatusTip(tr("Connect mysql database"));
    connect(connDatabaseAction, SIGNAL(triggered()), this, SLOT(onConnDatabase()));

    loadFormatFileAction = new QAction(tr("Load Format File"), this);
    loadFormatFileAction->setStatusTip(tr("Load Format File"));
    connect(loadFormatFileAction, SIGNAL(triggered()), this, SLOT(onLoadFormatFile()));

    editLogicFileAction = new QAction(tr("Edit Logic File"), this);
    editLogicFileAction->setStatusTip(tr("Edit Logic File"));
    connect(editLogicFileAction, SIGNAL(triggered()), this, SLOT(onEditLogicFile()));

    // system
    viewMenu = new QToolButton(this);
    viewMenu->setPopupMode(QToolButton::MenuButtonPopup);
    viewMenu->setIcon(QIcon(":/icon/action/viewMenu.png"));
    viewMenu->setMenu(createViewMenu());

    aboutAction = new QAction(QIcon(":/icon/action/about.png"), tr("&About"), this);
    aboutAction->setStatusTip(tr("About ISR Reader"));
    connect(aboutAction, SIGNAL(triggered()), this, SLOT(onAbout()));

    exitAction = new QAction(QIcon(":/icon/action/quit.png"), tr("E&xit"), this);
    exitAction->setShortcuts(QKeySequence::Quit);
    exitAction->setStatusTip(tr("Quit ISR Reader"));
    connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
}

void MainWindow::createToolbars()
{
    fileToolBar = addToolBar(tr("File"));
    fileToolBar->addAction(loginModeAction);
    fileToolBar->addSeparator();
    fileToolBar->addAction(connDeviceAction);
    fileToolBar->addAction(scanDeviceAction);

    editToolBar = addToolBar(tr("Edit"));
    editToolBar->addAction(connDatabaseAction);
    editToolBar->addAction(loadFormatFileAction);
    editToolBar->addAction(editLogicFileAction);

    systemToolBar = addToolBar(tr("System"));
    systemToolBar->addWidget(viewMenu);
    systemToolBar->addAction(aboutAction);
    systemToolBar->addAction(exitAction);
}

void MainWindow::createPropertyPlane()
{
    auto *deviceSetting = new QWidget(this);
    auto *deviceSettingLayout = new QVBoxLayout(this);

    deviceStateImage = new QToolButton(this);
    deviceStateImage->setIcon(QIcon(":/icon/propertyplane/devicework.jpeg"));
    deviceStateImage->setIconSize(QSize(150, 120));
    deviceStateImage->setCheckable(true);

    // conn setting
    auto *connSettingGroupBox = new QGroupBox(tr("Conn Setting:"), this);
    auto *connSettingGroupLayout = new QGridLayout(this);
    connSettingGroupLayout->addWidget(new QLabel(tr("No.")), 0, 0, Qt::AlignLeft);

    deviceNoComboBox = new QComboBox(this);
    deviceNoComboBox->setEditable(false);
    deviceNoComboBox->addItem("ISR_5K");
    deviceNoComboBox->addItem("ISR_6K");
    deviceNoComboBox->addItem("ISR_8K");
    deviceNoComboBox->addItem("ISR_9K");
    deviceNoComboBox->addItem("ISR_8050");
    deviceNoComboBox->addItem("ISR_6050");
    deviceNoComboBox->addItem("ISR_8050B");
    deviceNoComboBox->addItem("ISR_2050");
    deviceNoComboBox->addItem("ISR_2000A");
    deviceNoComboBox->addItem("ISR_2000B");
    deviceNoComboBox->addItem("OMR_3000");
    deviceNoComboBox->addItem("ISR_7050");
    deviceNoComboBox->addItem("CANON_NDR");
    deviceNoComboBox->addItem("ISR_TWAIN");
    deviceNoComboBox->addItem("EPTWAIN");
    connSettingGroupLayout->addWidget(deviceNoComboBox, 0, 1, Qt::AlignLeft);

    connSettingGroupLayout->addWidget(new QLabel(tr("Ip.")), 1, 0, Qt::AlignLeft);
    deviceIpAddrComboBox = new QComboBox(this);
    deviceIpAddrComboBox->setEditable(true);
    deviceIpAddrList.push_back("127.0.0.1");
    deviceIpAddrList.push_back("192.168.0.154");
    for (auto& ipaddr : deviceIpAddrList)
        deviceIpAddrComboBox->addItem(ipaddr);
    //QIntValidator *validator = new QIntValidator(2, 64, this);
    //fontSizeCombo->setValidator(validator);
    connect(deviceIpAddrComboBox, SIGNAL(currentIndexChanged(QString)),
            this, SLOT(onDeviceIpAddrChanged(QString)));
    connSettingGroupLayout->addWidget(deviceIpAddrComboBox, 1, 1, Qt::AlignLeft);
    connSettingGroupBox->setLayout(connSettingGroupLayout);

    // scan setting
    auto *scanSettingGroupBox = new QGroupBox(tr("Scan Setting:"), this);
    auto *scanSettingGroupLayout = new QGridLayout(this);
    scanSettingGroupLayout->addWidget(new QLabel(tr("Speed.")), 0, 0, Qt::AlignLeft);

    scanSpeedComboBox = new QComboBox(this);
    scanSpeedComboBox->setEditable(false);
    scanSpeedComboBox->addItem(tr("Nomal(2.2)"));
    scanSettingGroupLayout->addWidget(scanSpeedComboBox, 0, 1, Qt::AlignLeft);

    scanSettingGroupLayout->addWidget(new QLabel(tr("Card Inter.")), 1, 0, Qt::AlignLeft);
    cardIntervalComboBox = new QComboBox(this);
    cardIntervalComboBox->setEditable(false);
    cardIntervalComboBox->addItem("10");
    cardIntervalComboBox->addItem("20");
    cardIntervalComboBox->addItem("40");
    cardIntervalComboBox->addItem("60");
    cardIntervalComboBox->addItem("80");
    cardIntervalComboBox->addItem("100");

    connect(cardIntervalComboBox, SIGNAL(currentIndexChanged(QString)),
            this, SLOT(onDeviceIpAddrChanged(QString)));
    scanSettingGroupLayout->addWidget(cardIntervalComboBox, 1, 1, Qt::AlignLeft);
    scanSettingGroupBox->setLayout(scanSettingGroupLayout);

    deviceSettingLayout->addWidget(deviceStateImage);
    deviceSettingLayout->addWidget(connSettingGroupBox);
    deviceSettingLayout->addWidget(scanSettingGroupBox);
    deviceSettingLayout->addStretch();
    deviceSetting->setLayout(deviceSettingLayout);

    auto *editSetting = new QWidget(this);
    auto *editSettingLayout = new QVBoxLayout(this);

    QGroupBox *modeSettingGroup = new QGroupBox(tr("mode setting"));
    QVBoxLayout *modeLayout = new QVBoxLayout;
    exam_room_mode_radio_btn = new QRadioButton(tr("exam room mode"));
    connect(exam_room_mode_radio_btn, SIGNAL(toggled(bool)), this, SLOT(showExamRoomModePage(bool)));
    insert_mode_radio_btn = new QRadioButton(tr("insert mode"));
    connect(insert_mode_radio_btn, SIGNAL(toggled(bool)), this, SLOT(showInsertModePage(bool)));

    modeLayout->addWidget(exam_room_mode_radio_btn);
    modeLayout->addWidget(insert_mode_radio_btn);
    modeSettingGroup->setLayout(modeLayout);

    editSettingLayout->addWidget(modeSettingGroup);
    editSettingLayout->addStretch();
    editSetting->setLayout(editSettingLayout);

    auto *dataSetting = new QWidget(this);
    auto *systemSetting = new QWidget(this);

    propertyPlane = new QToolBox(this);
    propertyPlane->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Ignored));
    propertyPlane->setMinimumWidth(deviceSetting->sizeHint().width());

    propertyPlane->addItem(deviceSetting, tr("Device Setting"));
    propertyPlane->addItem(editSetting, tr("Exam Setting"));
    propertyPlane->addItem(dataSetting, tr("Paper Setting"));
    propertyPlane->addItem(systemSetting, tr("System Setting"));
}

void MainWindow::createLogTabWidget()
{
    logTabWidget = new QTabWidget(this);
    logTabWidget->setTabPosition(QTabWidget::South);
    logTabWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
    logTabWidget->setMinimumHeight(150);

    auto *deviceLogPage = new QTextEdit(this);
    deviceLogPage->setReadOnly(true);
    deviceLogPage->setFont(QFont("宋体", 12));
    deviceLogPage->setAlignment(Qt::AlignLeft);

    auto normalImageFlag = QTextImageFormat();
    normalImageFlag.setName(":/icon/log/normal.png");
    normalImageFlag.setWidth(20);
    normalImageFlag.setHeight(20);

    auto errorImageFlag = QTextImageFormat();
    errorImageFlag.setName(":/icon/log/error.png");
    errorImageFlag.setWidth(20);
    errorImageFlag.setHeight(20);

    for(auto i = 0; i < 10; ++i)
    {
        deviceLogPage->setTextColor(Qt::black);
        for(auto j = 0; j < 5; ++j)
        {
            deviceLogPage->append("2024:01:22 00:05:23 test log...");
        }

        deviceLogPage->setTextColor(Qt::red);
        for(auto k = 0; k < 3; ++k)
        {
            deviceLogPage->append("2024:01:22 00:05:23 test error...");
        }
    }

//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");
//    deviceLogPage->moveCursor(QTextCursor::StartOfLine);
//    deviceLogPage->textCursor().insertImage(normalImageFlag);

//    deviceLogPage->append("2024:01:22 00:05:23 test log...");
//    deviceLogPage->moveCursor(QTextCursor::StartOfLine);
//    deviceLogPage->textCursor().insertImage(normalImageFlag);

//    deviceLogPage->append("2024:01:22 00:05:23 test log...");
//    deviceLogPage->moveCursor(QTextCursor::StartOfLine);
//    deviceLogPage->textCursor().insertImage(normalImageFlag);

//    deviceLogPage->append("2024:01:22 00:05:23 test log...");
//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->moveCursor(QTextCursor::Left);
//    deviceLogPage->textCursor().insertImage(normalImageFlag);
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(errorImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::red);
//    deviceLogPage->append("2024:01:22 00:05:23 test error...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(errorImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::red);
//    deviceLogPage->append("2024:01:22 00:05:23 test error...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(errorImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::red);
//    deviceLogPage->append("2024:01:22 00:05:23 test error...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(errorImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::red);
//    deviceLogPage->append("2024:01:22 00:05:23 test error...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->moveCursor(QTextCursor::NextRow);
//    deviceLogPage->textCursor().insertImage(errorImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::red);
//    deviceLogPage->append("2024:01:22 00:05:23 test error...");

//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->textCursor().insertImage(errorImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::red);
//    deviceLogPage->append("2024:01:22 00:05:23 test error...");

//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->textCursor().insertImage(errorImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::red);
//    deviceLogPage->append("2024:01:22 00:05:23 test error...");

//    deviceLogPage->textCursor().insertImage(normalImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::black);
//    deviceLogPage->append("2024:01:22 00:05:23 test log...");

//    deviceLogPage->textCursor().insertImage(errorImageFlag, QTextFrameFormat::Position(1));
//    deviceLogPage->setTextColor(Qt::red);
//    deviceLogPage->append("2024:01:22 00:05:23 test error...");

    logTabWidget->addTab(deviceLogPage, tr("device log"));
    logTabWidget->addTab(new QTextEdit(this), tr("ocr log"));
    logTabWidget->addTab(new QTextEdit(this), tr("nta log"));
}

void MainWindow::createWorkSpace()
{
    auto *blankpage = new QGraphicsView(this);

//    QTableView *insertPaperPage = new QTableView(this);
//    insertPaperPage->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
//    //insertPaperPage->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
//    // Create the data model:
//    auto model = new QSqlRelationalTableModel(insertPaperPage);
//    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
//    model->setTable("books");

//    // Remember the indexes of the columns:
//    auto authorIdx = model->fieldIndex("author");
//    auto genreIdx = model->fieldIndex("genre");

//    // Set the relations to the other database tables:
//    model->setRelation(authorIdx, QSqlRelation("authors", "id", "name"));
//    model->setRelation(genreIdx, QSqlRelation("genres", "id", "name"));

//    // Set the localized header captions:
//    model->setHeaderData(authorIdx, Qt::Horizontal, tr("Author Name"));
//    model->setHeaderData(genreIdx, Qt::Horizontal, tr("Genre"));
//    model->setHeaderData(model->fieldIndex("title"),
//                         Qt::Horizontal, tr("Title"));
//    model->setHeaderData(model->fieldIndex("year"), Qt::Horizontal, tr("Year"));
//    model->setHeaderData(model->fieldIndex("rating"),
//                         Qt::Horizontal, tr("Rating"));

//    // Populate the model:
//    if (!model->select()) {
//        showError(model->lastError());
//        //return insertPaperPage;
//        return;
//    }
//    //showError(model->lastError());
//    // Set the model and hide the ID column:
//    insertPaperPage->setModel(model);
//    insertPaperPage->setItemDelegate(new BookDelegate(insertPaperPage));
//    insertPaperPage->setColumnHidden(model->fieldIndex("id"), true);
//    insertPaperPage->setSelectionMode(QAbstractItemView::SingleSelection);

//    // Lock and prohibit resizing of the width of the rating column:
//    insertPaperPage->horizontalHeader()->setSectionResizeMode(
//                model->fieldIndex("rating"),
//                QHeaderView::ResizeToContents);

//    //insertPaperPage->setCurrentIndex(model->index(0, 0));
//    //return insertPaperPage;
    workspace = new QStackedWidget(this);
    workspace->addWidget(blankpage);
//    workspace->addWidget(insertPaperPage);
}

void MainWindow::showMessage(QString const& msg)
{
    QMessageBox::information(this, "Hi", msg);
}

QMenu *MainWindow::createViewMenu()
{
    auto *showPropertyPlaneAction = new QAction(tr("Show property plane"));
    connect(showPropertyPlaneAction, SIGNAL(triggered()), this, SLOT(showPropertyPlane()));
    auto *showLogWindowAction = new QAction(tr("Show output window"));
    connect(showLogWindowAction, SIGNAL(triggered()), this, SLOT(showLogWindow()));

    auto *viewMenu = new QMenu;
    viewMenu->addAction(showPropertyPlaneAction);
    viewMenu->addAction(showLogWindowAction);
    return viewMenu;
}

void MainWindow::showError(const QSqlError &err)
{
    QMessageBox::critical(this, "Unable to initialize Database",
                "Error initializing database: " + err.text());
}
